<template>
    <div  ref="swiper" id="products-swiper">


        <div  ref="content" class="content">
            <div class="product" v-for="(product, index) in products" :key="index">
                <div class="photo">
                    <img :src="(product.photoUrl as any)" alt="">
                    <div class="name">
                        {{ product.name }}
                    </div>
                </div>

            </div>
        </div>

        <div @click="Trans2Pre" class="swiper-btn pre-btn">
            <img src="/index/test.png" alt="">
        </div>

        <div @click="Trans2Next" class="swiper-btn next-btn">
            <img src="/index/test.png" alt="">
        </div>
    </div>
</template>

<script setup lang="ts">
import { defineProps, PropType, onMounted, ref } from 'vue';
import { Product } from '../../store/index'
import { propsToAttrMap } from '@vue/shared';
const props = defineProps({
    products: {
        type: Array as PropType<Product[]>,
        default: new Array<Product>
    }
})

let scroll_x = ref(0)
let swiper: HTMLDivElement = ref(null).value as unknown as HTMLDivElement
let content:HTMLDivElement = ref(null).value as unknown as HTMLDivElement
onMounted(() => {
    window.onresize = ()=>{
        console.log(swiper.offsetWidth);
        swiper.style.height =( swiper.offsetWidth / 5)*1.7 + "px"
        
    }
})

const Trans2Pre = () => {
    if(scroll_x.value == 0){
        return
    }
    --scroll_x.value
    console.log( (swiper.offsetWidth / 5) * scroll_x.value);
    content.scroll({
        left:(swiper.offsetWidth / 5) * scroll_x.value,
        behavior:'smooth'
    })
}
const Trans2Next = () => {
    console.log(scroll_x.value, props.products.length);
    
    if(scroll_x.value == props.products.length - 5){
        content.scroll({
        left:0,
        behavior:'smooth'
    })
        scroll_x.value = 0
        return
    }
    ++scroll_x.value
    console.log((swiper.offsetWidth / 5) * scroll_x.value );
    content.scroll({
        left:(swiper.offsetWidth / 5) * scroll_x.value,
        behavior:'smooth'
    })
}
</script>

<style lang="less" scoped>
#products-swiper {
    height: 60vh;
    width: 100%;
    margin-top: 1rem;
    background-color: rgb(140, 140, 140);
    position: relative;

    .swiper-btn {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 5rem;
        height: 5rem;
        background-color: rgb(255, 255, 255);
        border-radius: 100%;
        cursor: pointer;

        img {
            height: 100%;
            width: 100%;
        }
    }

    .pre-btn {
        left: 2rem;
    }

    .next-btn {
        right: 2rem;
    }

    .content {
        width: 100%;
        height: 100%;
        // padding: 1rem;
        background-color: rgb(255, 255, 255);
        overflow: hidden;
        // overflow-x: scroll;
        // overflow-y: hidden;
        white-space: nowrap;

        .product {
            display: inline-block;

            width: 20%;
            height: 100%;
            padding: 1rem;


            .photo {
                position: relative;
                width: 100%;
                height: 100%;
                background-color: rgb(205, 205, 205);
                display: flex;
                justify-content: center;
                align-items: center;
                overflow: hidden;



                img {
                    height: 100%;
                    width: 100%;
                    transition: all 0.2s;
                }

                .name {
                    position: absolute;
                    padding: 1rem;

                    width: 80%;
                    left: 50%;
                    transform: translateX(-50%);
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    bottom: 2rem;
                    background-color: rgba(69, 171, 231, 0.8);
                    font-size: 1.8rem;
                    color: white;
                    cursor: pointer;
                    opacity: 0;
                    transition: all 0.2s;
                }



            }

            :hover {
                .name {
                    opacity: 1;
                    transition: all 0.2s;
                }

                img {
                    width: 120%;
                    height: 120%;
                    transition: all 0.2s;
                }
            }

            .name:hover {
                text-decoration: underline;
            }
        }
    }

    .content::-webkit-scrollbar {
        display: none;
    }
}
</style>
